<template>
  <div>
    <FormPanel ref="formPanel" :list="formList" @formChange="formChangeHandle" :isSubmitBtn="false" :collapse="false" labelWidth="110" />
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'center'
      }"
    >
      <el-button @click="closeDrawer">关 闭</el-button>
      <multiuse-button type="primary" :disabled="this.billNos.length == 0" :click="commitHandle">确 定</multiuse-button>
    </div>

    <!-- 三方票号查询 -->
    <BaseDialog :visible.sync="visible" title="三方票号查询" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <InvoiceNum :rowData="formData" @close="emitCloseHandler" />
    </BaseDialog>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { changeBill } from '@/api/omFinance/FinanceManage/LicenseBindingQuery';
import InvoiceNum from './InvoiceNum';

export default {
  mixins: [dictionary],
  props: ['rowData'],

  components: {
    InvoiceNum
  },
  data() {
    this.saveData = null;
    return {
      formList: this.createdFrom(),
      fetchParams: {},
      fetchApi: () => {},
      formData: {},
      visible: false,
      doNos: [],
      billNos: []
    };
  },
  mounted() {
    this.init();
    console.log(this.rowData, '---奥利给---');
  },
  methods: {
    init() {
      if (this.rowData.listData.length > 0) {
        let data = this.rowData.listData;
        let totalAmount = 0;
        data.map(x => {
          totalAmount += x.chngAmount;
          this.doNos.push(x.doNo);
        });
        this.formList.find(x => x.fieldName == 'chngAmounts').initialValue = totalAmount;
      }
    },
    searchFun() {
      this.visible = true;
    },
    emitCloseHandler(val, data) {
      this.visible = val;
      let totalAmount = 0;
      this.billNos = [];
      if (data) {
        data.map(x => {
          totalAmount += x.availableAmount;
          this.billNos.push(x.billNo);
        });
        this.formList.find(x => x.fieldName == 'billNo').initialValue = this.billNos.toString();
        this.formList.find(x => x.fieldName == 'availableAmounts').initialValue = totalAmount;
      }
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    async commitHandle() {
      const res = await changeBill({ doNos: this.doNos, billNos: this.billNos });
      if (res.resultCode == 200) {
        await this.closeDrawer('refresh');
      }
    },
    closeDrawer(type) {
      this.$emit('close', false, type);
    },
    createdFrom() {
      return [
        {
          type: 'INPUT',
          label: '已选订单总金额',
          fieldName: 'chngAmounts',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '新三方票号',
          fieldName: 'billNo',
          placeholder: '请选择',
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.searchFun()}></el-button>
              </div>
            );
          }
        },
        {
          type: 'INPUT',
          label: '可用余额',
          fieldName: 'availableAmounts',
          placeholder: '',
          disabled: true
        }
      ];
    }
  }
};
</script>
